#include "QbTFBondModule.h"
#include "QbTFBondDefines.h"
#include "QbTFMessageProcess.h"
#include "QbTFBondIntegrated.h"
#include <qbtools/message/RequestNotifier.h>
#include <qbkernel/include/qb_base.h>
#include <uam/ServerTimeMgr.h>
#include <uam/UserAccountManager.h>
#include <bonddict/IBondDict.h>
#include <include/CmdDefines.h>
#include <QWidget>

using namespace qb;

extern "C" S_QBTFBOND_EXPORT IModule * getModule()
{
	return &QbTFBondModule::instance();
}

QbTFBondModule& QbTFBondModule::instance()
{
	static QbTFBondModule s_module;
	return s_module;
}

void QbTFBondModule::initialize(IApplication* app)
{
    m_app = app;

	registNetEvent();
//	qbbond::InitDecodeFuncs();
	//TODO: 需要在线程中运行，并且等待权限获取完成
	requestTFBondData();
}

void QbTFBondModule::unInitialize()
{
	unRegistNetEvent();
//	unRegistSpecificMsgProcessor();
}

int QbTFBondModule::execCommand(CmdInfo ci) const
{
	IMainFrame* frame = m_app->mainFrame();
	QString paraStr = QString::fromUtf8(ci.parameters);
	QString titleStr = QString::fromUtf8(ci.title);


     //模块可以自己决定是否多开
    int tabId = frame->existCmdWindow(ci.cmdId, paraStr, ci.raw_param);
	if (tabId != -1)
	{
		frame->activateCmdWindow(tabId);
	}
	else
	{
        QWidget* widget = nullptr;
        int actualCmdId = ci.actualCmdId;

		if (CMD_TreasuryFutures == ci.cmdId)
		{
			widget = new QbTFBondIntegrated(nullptr);
		}

		if (widget)
			frame->openCmdWindow(ci.cmdId, paraStr, widget, titleStr);
	}
	return 0;
}

bool QbTFBondModule::closeCommandWindow(int cmdId, QWidget* widget)
{
	if (!widget)
		return true;

	widget->setParent(nullptr);
	delete widget;

	return true;
}

void QbTFBondModule::requestTFBondData()
{

}

void QbTFBondModule::registNetEvent()
{
	qbBase::getNetworkService()->RegisterNetworkEventObserver(this);
}

void QbTFBondModule::unRegistNetEvent()
{
	qbBase::getNetworkService()->DeRegisterNetworkEventObserver(this);
}

STDMETHODIMP_(void) QbTFBondModule::OnRecover(THIS_ int32_t connect_id)
{
	requestTFBondData();
}
